/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.treeeditpart;

import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.RootEditPart;
import org.eclipse.gef.editparts.AbstractTreeEditPart;
import org.eclipse.gef.editpolicies.RootComponentEditPolicy;
import org.eclipse.swt.graphics.Image;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.IUMLObjectEditPart;

/**
 * Basic implementation of an EditPart for an UML object. This editpart is designed 
 * to be used in the outline
 */
public class CommonTreeEditPart extends AbstractTreeEditPart implements IUMLObjectEditPart {

	/** ModelManager used by the editor for which the outline is displayed */
	protected ModelManager modelManager;
	
	/**
	 * Create a new instance of this edit part using the given model element.
	 * @param element a non-null ShapesDiagram instance
	 * @param modelManager  the model manager associated to the model
	 */
	public CommonTreeEditPart(Object element,ModelManager modelManager) {
		super(element);
		this.modelManager=modelManager;		
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Image getImage() {
		return modelManager.getLabelProvider().getImage(getModel());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getText() {
		return modelManager.getLabelProvider().getText(getModel());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void createEditPolicies() {
		// If this editpart is the root content of the viewer, then disallow removal
		if (getParent() instanceof RootEditPart) {
			installEditPolicy(EditPolicy.COMPONENT_ROLE, new RootComponentEditPolicy());
		}
	}	

	/**
	 * {@inheritDoc}
	 */
	public boolean testAttribute(Object target, String name, String value) {
		if (target instanceof IUMLObjectEditPart) {
			return umlFilter.testAttribute(target, name, value);
		}
		return false;
	}
	
	/**
	 * {@inheritDoc} 
	 */
	@Override
	public RootEditPart getRoot() {
		// we reimplement this method because the parent maybe null
		// so in the case of GEF this method can bug the application
		// because they do not test getParent==null
		return ( (getParent()!=null) ? getParent().getRoot() : null ); 
	}

	/**
	 * Returns the model manager associated to the actual editor
	 * @return the model manager associated to the actual editor
	 */
	public ModelManager getModelManager() {
		return modelManager;
	}
}